package aod.android.com.bernay.fixing.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import java.io.IOException;

import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.fixing.bean.FixingDatabean;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.util.SharePreUtils;
import chen.lion.hilib.util.LogUtils;
import chen.lion.hilib.view.bind.HiView;

public class FixingAddNameActivity extends BernayBaseActivity {
    //设备名字
    @HiView(R.id.fixing_ed_name)
    EditText fixing_ed_name;
    private String fixingName;
    private String issetName = "";
    private String snNumber = "";
    private String checkCode = "";

    @Override
    protected int initLayout() {
        return R.layout.activity_fixing_add_name;
    }

    @Override
    protected void initData() {
        if(getIntent().getStringExtra("fixingname")!=null){
            issetName = getIntent().getStringExtra("fixingname");
        }

        if(getIntent().getStringExtra("SnNumber")!=null){
            snNumber = getIntent().getStringExtra("SnNumber");
        }
        if(getIntent().getStringExtra("CheckCode")!=null){
            checkCode = getIntent().getStringExtra("CheckCode");
        }
        if(issetName.equals("0")){
            initTitle(getString(R.string.add_fixing_name), getString(R.string.add_fixing_save), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    fixingName = fixing_ed_name.getText().toString();
                    if(!fixingName.equals("")){
                        SharePreUtils.newInstance(FixingAddNameActivity.this).updateFixing(1);
                        AddFixing();
                    }else {
                        Toast.makeText(FixingAddNameActivity.this,"请填写设备名称",Toast.LENGTH_LONG).show();
                    }
                }
            });
        }else if(issetName.equals("1")){
            initTitle(getString(R.string.alter_add_fixing_name), getString(R.string.add_fixing_save), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    fixingName = fixing_ed_name.getText().toString();
                    if(!fixingName.equals("")){
                        finish();
                    }else {
                        Toast.makeText(FixingAddNameActivity.this,"请填写设备名称",Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    }

    private void AddFixing() {
        BernayApi.http().addFixing("SS201905240940WS", fixingName, checkCode, new BernayCallback() {
            @Override
            public void onJsonData(String json) {
                    Gson gson = new Gson();
                    FixingDatabean fixingDatabean = gson.fromJson(json, FixingDatabean.class);
                    if(fixingDatabean!=null&&fixingDatabean.getStatus()==1){
                        showToast("添加设备成功");
                        finish();
                    }else {
                        showToast("添加设备失败");
                    }
            }
            @Override
            public void onHttpFailure(IOException e) {
                showToast("添加设备失败");
            }
        });



    }
}
